<script setup>
  import { ref } from 'vue'
  import TaskAddForm from './TaskAddForm.vue'
  import { useTaskConfigStore, useTaskStore } from '@/stores/task'

  const props = defineProps({
    initialData: Object,
    url: String,
    formConfigUrl: String,
  })
  const emit = defineEmits(['save', 'cancel'])

  const formConfigStore = useTaskConfigStore()
  formConfigStore.setUrl(props.formConfigUrl)
  const loading = ref(true)
  const preload = async () => {
    const request = formConfigStore.loadConfig()
    request.then((config) => {
      loading.value = false
    })
  }

  const taskStore = useTaskStore()
  taskStore.setAddUrl(props.url)
  const onSaved = (task) => emit('save', task)
  const onCancel = () => emit('cancel')
  await preload()
</script>
<template>
  <div v-if="loading">Chargement des informations</div>

  <TaskAddForm
    v-else
    :initial-data="initialData"
    v-on:save="onSaved"
    v-on:cancel="onCancel"
  ></TaskAddForm>
</template>
